﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manager_WebServices_Reports : System.Web.UI.Page {
	public string view { get { return Request["view"] + ""; } }
	public int top { get { return int.Parse("0" + Request["top"]); } }
	protected void Page_Load(object sender, EventArgs e) {
		var nl = "";// Environment.NewLine;
		using (SqlConnection conn = new SqlConnection(new Dimok.Aspx.Manager.UOX().db)) {
			conn.Open();
			if (!new Data.LINQ.MotoBank().IsReport(view).Value)
				throw new Exception("View " + view + " is not a report.");
			SqlCommand cmd = new SqlCommand("SELECT " + (top > 0 ? " TOP(" + top + ")" : "") + " * FROM " + view, conn);
			SqlDataReader reader = cmd.ExecuteReader();
			do {
				//Response.ContentType = "application/vnd.ms-excel";
				Response.Clear();
				Response.Buffer = false;
				Response.Write("<TABLE>"+nl);
				Response.Write("<TR>" + nl);
				for (int i = 0; i < reader.FieldCount; i++)
					Response.Write("<TH>" + reader.GetName(i) + "</TH>" + nl);

				Response.Write("</TR>" + nl);
				while (reader.Read()) {
					Response.Write("<TR>" + nl);
					for (int i = 0; i < reader.FieldCount; i++)
						Response.Write("<TD>" + HttpUtility.HtmlEncode(reader[i] + "") + "</TD>" + nl);
					Response.Write("</TR>" + nl);
				}
				Response.Write("</TABLE>");
			} while (reader.NextResult());
			Response.End();
		}

	}
}
